package com.bum.api.processor.rss.html.bo;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.bum.api.bo.RssBO;
import com.bum.api.bo.om.RssEntryExt;
import com.bum.api.core.exp.om.Metas;
import com.bum.api.core.exp.om.Params;
import com.bum.api.core.html.HtmlModel;
import com.bum.api.core.html.HtmlRender;
import com.bum.api.core.util.render.MeRenderUtil;
import com.bum.rss.exp.om.Rss;
import com.bum.rss.exp.om.RssEntry;

public class RssHtmlViewBO {
	HtmlRender		htmlRender;
	RssBO			rssBO;

	public String sitemap(String path, Params params, Metas metas) {
		HtmlModel model = HtmlModel.get(metas);

		List<RssEntry> list = rssBO.getSitemap();
		list.addAll(rssBO.load("baomoi/src", null).getEntries());
		model.put("cats", list);
		model.put("urlHelper", RssUrlHelper.get());

		return htmlRender.render("rss/rss-sitemap", model);
	}

	public String sites(String path, Params params, Metas metas) {
		String userName = metas.get(Metas.OLA_ACC);

		String url = null;

		HtmlModel model = HtmlModel.get(metas);

		Rss rss = rssBO.load(url, userName);
		model.put("links", rss.getEntries());
		model.put("urlHelper", RssUrlHelper.get());

		return htmlRender.render("rss/rss-sites", model);
	}

	public String view(String path, Params params, Metas metas) {
		String userName = metas.get(Metas.OLA_ACC);

		String url = path;

		HtmlModel model = HtmlModel.get(metas);

		// load rss
		Rss rss = rssBO.load(url, userName);

		String uniqueUrl = url;
		if (uniqueUrl != null) {
			String[] parts = StringUtils.split(uniqueUrl, "/", 3);
			if (parts.length == 3) {
				parts[1] = "m";
				uniqueUrl = StringUtils.join(parts, "/");
			}
		}
		model.put("uniqueUrl", "http://bum.vn/app/rss/" + StringUtils.trimToEmpty(uniqueUrl));
		model.put("url", "http://bum.vn/app/rss/" + StringUtils.trimToEmpty(url));
		model.put("urlHelper", RssUrlHelper.get());

		// nội dung không tồn tại
		if (rss == null) {
			return htmlRender.render("rss/view-null", model);
		}

		if (rss.getEntry() != null) {
			model.put("title", rss.getEntry().getTitle());
		}

		// xử lý rss content
		if (rss.getType() == Rss.TYPE_CONTENT) {
			return showContent(userName, rss, model);
		}

		// xử lý links
		return showLinks(rss, model);
	}

	private String showLinks(Rss rss, HtmlModel model) {
		List<RssEntry> entries = rss.getEntries();
		if (entries != null && entries.size() > 0) {
			convertLinks(model, entries, "links");
		}

		return htmlRender.render("rss/view-links", model);
	}

	public static void convertLinks(HtmlModel model, List<RssEntry> entries, String modelName) {
		boolean hasMeta = false;

		List<RssEntryExt> links = new ArrayList<RssEntryExt>(entries.size());
		for (RssEntry entry : entries) {
			RssEntryExt re = new RssEntryExt(entry);
			links.add(re);

			hasMeta |= re.isMeta();
		}

		model.put("hasMeta", hasMeta);
		model.put(modelName, links);
	}

	private String showContent(String userName, Rss rss, HtmlModel model) {
		if (rss == null || rss.getEntry() == null) {
			return htmlRender.render("rss/view-article", model);
		}
		
		String desc = rss.getEntry().getContent();
		int idx = desc.indexOf("\n");
		if (idx > 0) {
			desc = desc.substring(0, idx);
		}
		if (desc.length() > 250) {
			desc = desc.substring(0, desc.lastIndexOf(" "));
		}
		model.put("desc", desc);

		String content = prepareContent(rss.getEntry().getContent());
		model.put("content", content);

		return htmlRender.render("rss/view-article", model);
	}

	public static String prepareContent(String content) {
		if (StringUtils.isBlank(content)) {
			return "";
		}

		content = MeRenderUtil.renderContent(content);
		content = "<p>" + content.replace("<br/>", "</p><p>") + "</p>";
		content = content.replace("<p></p>", "");
		return content;
	}

	/**
	 * SETTER
	 */
	public void setRssBO(RssBO rssBO) {
		this.rssBO = rssBO;
	}

	public void setHtmlRender(HtmlRender htmlRender) {
		this.htmlRender = htmlRender;
	}
}
